package com.zhang.satokendemo.satoken;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhang.satokendemo.entity.SysMenu;
import com.zhang.satokendemo.entity.SysRole;
import com.zhang.satokendemo.entity.SysRoleMenu;
import com.zhang.satokendemo.entity.SysUserRole;
import com.zhang.satokendemo.service.ISysMenuService;
import com.zhang.satokendemo.service.ISysRoleMenuService;
import com.zhang.satokendemo.service.ISysRoleService;
import com.zhang.satokendemo.service.ISysUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 自定义权限验证
 */
@Component
@Slf4j
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private ISysUserRoleService userRoleService;

    @Resource
    private ISysRoleMenuService roleMenuService;

    @Resource
    private ISysRoleService roleService;

    @Resource
    private ISysMenuService menuService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        //通过loginId拿到权限列表
        List<SysUserRole> list = userRoleService.list(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, loginId));
        Set<Integer> roleIds = list.stream().map(sysUserRole -> sysUserRole.getRoleId()).collect(Collectors.toSet());
        List<SysRoleMenu> roleMenus = roleMenuService.list(Wrappers.<SysRoleMenu>lambdaQuery().in(SysRoleMenu::getRoleId, roleIds));
        List<Integer> menuIds = roleMenus.stream().map(sysRoleMenu -> sysRoleMenu.getMenuId()).collect(Collectors.toList());
        List<String> collect = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menuIds)) {
            List<SysMenu> menus = menuService.list(Wrappers.<SysMenu>lambdaQuery().in(SysMenu::getMenuId, menuIds));
            collect = menus.stream().map(menu -> menu.getResource()).collect(Collectors.toList());
        }
        log.info("获取用户权限列表:{}",collect);
        StpUtil.getTokenSession().set(SaSession.PERMISSION_LIST,collect);
        return collect;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        //返回此loginId得角色列表 本系统loginId用的是userId
        List<SysUserRole> list = userRoleService.list(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, loginId));
        List<Integer> roleIds = list.stream().map(sysUserRole -> sysUserRole.getRoleId()).collect(Collectors.toList());
        List<SysRole> roles = roleService.list(Wrappers.<SysRole>lambdaQuery().in(SysRole::getRoleId, roleIds));
        List<String> collect = roles.stream().map(role -> role.getRoleName()).collect(Collectors.toList());
        log.info("获取用户角色列表:{}",collect);
        StpUtil.getTokenSession().set(SaSession.ROLE_LIST,collect);
        return collect;
    }
}
